package com.yc.cloud.admin.controller.client;

import com.yc.cloud.admin.dto.request.AlarmRecordUploadReqDTO;
import com.yc.cloud.admin.dto.request.AlarmRecordUploadFileReqDTO;
import com.yc.cloud.admin.dto.request.HwDeviceRegisterReqDTO;
import com.yc.cloud.admin.service.AlarmRecordService;
import com.yc.cloud.admin.service.ClientApiService;
import com.yc.cloud.admin.service.HwDeviceService;
import com.yc.cloud.admin.vo.AlarmRecordVo;
import com.yc.cloud.admin.vo.ClientStrategyVo;
import com.yc.cloud.admin.vo.ClientStrategy2Vo;
import com.yc.cloud.admin.vo.HwDeviceVo;
import com.yc.cloud.api.enums.HwDeviceType;
import com.yc.cloud.common.basic.api.ResponseResult;

import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 客户端 - API接口
 * </p>
 *
 * @author kinggu
 * @since 2025-07-16
 */
@RestController
@RequestMapping("api")
@Tag(name = "客户端 - API接口")
@Slf4j
public class ClientApiController {

    @Resource
    private ClientApiService clientApiService;

    @Resource
    private AlarmRecordService alarmRecordService;

    @Resource
    private HwDeviceService hwDeviceService;

    @Operation(summary = "设备注册")
    @PostMapping("/devices/register")
    public ResponseResult<HwDeviceVo> register(@Validated @RequestBody HwDeviceRegisterReqDTO registerReqDTO) {
        //如果设备类型为空，则默认为边缘设备
        if(registerReqDTO.getDeviceType() == null){
            registerReqDTO.setDeviceType(HwDeviceType.EDGE_DEVICE.getCode());
        }
        HwDeviceVo hwDeviceVo = hwDeviceService.register(registerReqDTO);
        return ResponseResult.success(hwDeviceVo);
    }

    @Deprecated
    @PostMapping("/devices/{deviceSn}/strategy")
    @Operation(summary = "根据设备SN查询其应用的策略和模型URL(已废弃，请使用getStrategyByDeviceSn2方法)")
    public ResponseResult<List<ClientStrategyVo>> getStrategyByDeviceSn(@PathVariable String deviceSn,
                                                                        @RequestBody(required = false) com.yc.cloud.admin.dto.request.ClientStrategyQueryParam param) {
        long start = System.currentTimeMillis();
        List<ClientStrategyVo> strategyVoList = clientApiService.getStrategyForDevice(deviceSn, param);
        long end = System.currentTimeMillis();
        System.out.println("getStrategyByDeviceSn 耗时: " + (end - start) + "ms");
        return ResponseResult.success(strategyVoList);
    }

    @PostMapping("/devices/{deviceSn}/strategy2")
    @Operation(summary = "获取设备配置信息")
    public ResponseResult<List<ClientStrategy2Vo>> getStrategyByDeviceSn2(@PathVariable String deviceSn,
                                                                        @RequestBody(required = false) com.yc.cloud.admin.dto.request.ClientStrategyQueryParam param) {
       
        log.info("getStrategyByDeviceSn2 deviceSn:{} param:{}", deviceSn, param);
        if(StrUtil.isEmpty(param.getAppId())){
            return ResponseResult.failed("appid不能为空");
        }
        long start = System.currentTimeMillis();
       List<ClientStrategy2Vo> strategyVoList = clientApiService.getStrategyForDevice2(deviceSn, param);
        long end = System.currentTimeMillis();
        System.out.println("getStrategyByDeviceSn2 耗时: " + (end - start) + "ms");
        return ResponseResult.success(strategyVoList);
    }



    @PostMapping("/alarms/upload")
    @Operation(summary = "上传报警信息")
    public ResponseResult<AlarmRecordVo> uploadAlarmInfo(@Validated @RequestBody AlarmRecordUploadReqDTO uploadReqDTO) {
        long start = System.currentTimeMillis();
        log.info("uploadAlarmInfo - alarmType:{}, deviceSn:{}, cameraId:{}, detectionPositionCount:{}", uploadReqDTO.getAlarmType(), uploadReqDTO.getDeviceSn(), uploadReqDTO.getCameraId(), uploadReqDTO.getDetectionPositions() == null ? 0 : uploadReqDTO.getDetectionPositions().size());
        AlarmRecordVo alarmRecordVo = clientApiService.uploadAlarmInfo(uploadReqDTO);
        long end = System.currentTimeMillis();
        System.out.println("uploadAlarmInfo 耗时: " + (end - start) + "ms");
        return ResponseResult.success(alarmRecordVo);
    }

    @PostMapping("/alarms/upload-file")
    @Operation(summary = "上传报警信息（文件上传版，便于Postman测试）")
    public ResponseResult<AlarmRecordVo> uploadAlarmInfoByFile(@Validated @ModelAttribute AlarmRecordUploadFileReqDTO uploadFileReqDTO) {
        long start = System.currentTimeMillis();
        AlarmRecordVo alarmRecordVo = clientApiService.uploadAlarmInfoByFile(uploadFileReqDTO);
        long end = System.currentTimeMillis();
        System.out.println("uploadAlarmInfoByFile 耗时: " + (end - start) + "ms");
        return ResponseResult.success(alarmRecordVo);
    }
}
